import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {QueryGoodsResponse} from "../apis/GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";

export function getMarketDeliveryOrderDetail(id: number) {
    return fetcher.get<MarketDeliveryOrderDetail>(`markets/deliveryOrders/${id}`, {
        includeItems: true,
        includeComparable:true,
    })
}

export interface QueryMarketOrderRequest {
    marketIds: number[]
    startCreatedDate?: string
    endCreatedDate?: string
    validatedStartDate?: string
    validatedEndDate?:string
    status?:MarketDeliveryOrderStatus
    number?:string
}

export function queryMarketDeliveryOrders(queryMarketOrderRequest: QueryMarketOrderRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryMarketDeliveryOrderResponse>>("markets/deliveryOrders", {...queryMarketOrderRequest, ...pageable})
}

export interface QueryMarketDeliveryOrderResponse{
    id:number
    createdDateTime: OffsetDateTime
    market: NumberIdAndName
    validatedDateTime?: OffsetDateTime
    status: MarketDeliveryOrderStatus
    hasDifference:boolean
    number:string
}

export function getMarketDeliveryOrderDetailWithoutItems(id: number) {
    return fetcher.get<MarketDeliveryOrderDetail>(`markets/deliveryOrders/${id}`)
}



export interface MarketDeliveryOrderDetail extends QueryMarketDeliveryOrderResponse{

    items: Array<MarketDeliveryOrderItem>
    auditedDateTime?: OffsetDateTime
    number:string
}


export interface MarketDeliveryOrderItem extends QueryGoodsResponse {
    amountInOrder: number
}

export interface AddMarketDeliveryOrderItemRequest {
    amount: number
    goodsId: number
    unitId?:number
}

export function addMarketDeliveryOrderItem(id: number, addMarketDeliveryOrderItemRequest: AddMarketDeliveryOrderItemRequest) {
    return fetcher.post(`markets/deliveryOrders/${id}/items`, {}, addMarketDeliveryOrderItemRequest)
}

export function deleteMarketDeliveryOrderItem(orderId: number, goodsId: number) {
    return fetcher.delete(`markets/deliveryOrders/${orderId}/items/${goodsId}`)
}

export interface UpdateMarketOrderItemAmountRequest {
    amount: number
    unitId?: number
}

export function updateMarketDeliveryOrderItemAmount(orderId: number, goodsId: number, updateMarketOrderItemAmountRequest: UpdateMarketOrderItemAmountRequest) {
    return fetcher.patch(`markets/deliveryOrders/${orderId}/items/${goodsId}/amount`, {}, updateMarketOrderItemAmountRequest)
}


export type MarketDeliveryOrderStatus = keyof typeof marketDeliveryOrderStatusMap

export const marketDeliveryOrderStatusMap =
    {
        "WAIT_VALIDATED": "待验收",
        "VALIDATED": "已验收",
        "AUDITED": "已审核",
    } as const

export const marketDeliveryOrderStatuses = Object.keys(marketDeliveryOrderStatusMap) as MarketDeliveryOrderStatus[]

export function compareWithOriginalOrder(orderId:number){
    return fetcher.get<MarketDeliveryOrderCompareWithOriginalOrderResponse>(`markets/deliveryOrders/${orderId}/compareWithOriginalOrder`, {})
}

export interface MarketDeliveryOrderCompareWithOriginalOrderResponse{
    marketId:number
    marketName:string
    items:MarketDeliveryOrderCompareWithOriginalOrderItem[]
}

export interface MarketDeliveryOrderCompareWithOriginalOrderItem{
    goodsId:number
    goodsName:string
    goodsBarcode:string
    amount?:number
    actuallyAmount?:number
}

export function auditMarketDeliveryOrder(orderId:number){
    return fetcher.patch<MarketDeliveryOrderCompareWithOriginalOrderResponse>(`markets/deliveryOrders/${orderId}/audit`, {})
}

export function clearMarketDeliveryOrderItems(orderId:number){
    return fetcher.post(`markets/deliveryOrders/${orderId}/items`)
}